/**
 * 支付页面
 */
Ext.define("PRO.shop.view.pay.payStyleView",{
    extend: 'Ext.panel.Panel',
    border:0,
    layout:'fit',
    alias: 'widget.shop.paystyleview',
    id:'shopPayStyleView',
    autoScroll : true,
    initComponent: function(){
        var me = this;
        me.bodyStyle="border-width:0;";
        var tbarHtml = '<font class="saleSuanban">账户余额支付</font>';
        me.tbar = {xtype:'box',cls:'playBuybox',html:tbarHtml};
        me.getData = me.getData();
        me.bbar = {
            xtype:'container',
            itemId:'payBox',
            cls:'payBoxcls',
            items:[{
                xtype:'button',
                cls:'btn JEPLUS_B_L10',
                width:100,
                style:'padding: 10px 12px !important;float:right;',
                text:'支付',
                handler:function(btn){
                    //请求弹框
                    //处理验证
                    if(me.getData.notenough){
                        JE.alert('请切换支付方式或充值');
                    }else{
                        var verification = document.querySelector('.verification').value;
                        if(JE.isEmpty(verification)){
                            return JE.msg('请输入验证码！')
                        }else{
                            me._thisMe.payTheOrder(me.orderId,verification,me._thisMe,me);
                        }
                    }
                }
            }]
        };
        me.items = [
            {
                xtype:'panel',
                layout:'hbox',
                itemId:'payStyle',
                border:false,
                items:[
                    {

                        xtype:'box',
                        itemId:'payStyleCon',
                        data:me.getData,
                        tpl:[
                            '<div>',
                            '<p class="paystyleTit">订单金额</p>',
                            '<p class="paystylemoney">￥{orderPay}</p>',
                            '<div class="aboutMoney">',
                                '<tpl if="notenough">',
                                    '<span class="notenough">账户余额不足</span>',
                                '</tpl>',
                                '<a class="recharge" herf="javascript:;">立即充值</a>',
                                '<a class="changePaystyle" herf="javascript:;">切换支付方式</a>',
                            '</div>',
                            '<p class="paystyleTit">账户余额</p>',
                            '<p class="paystylemoney">￥{accountBalance}</p>',
                            '<tpl if="!notenough">',
                            '<p class="paystyleTit">手机验证码</p>',
                            '<p>',
                                '<input class="verification" type="text" placeholder="请输入验证码" value={verification} >',
                            '</p>',
                            '<tpl if="verificationtext">',
                                '<p class="verificationtext">验证码已下发至<i>{verificationtext}</i>请注意查收!</p>',
                            '</tpl>',
                            '</tpl>',
                        '</div>'
                        ]
                    },
                    {
                        xtype:'button',
                        text:'获取验证码',
                        itemId:'getVerBtn',
                        cls:'btn JEPLUS_B_L10',
                        isClick:true,
                        hidden:me.getData.notenough,
                        margin:'180px 0 0 50px',
                        handler:function(btn){
                            me.getVer(btn)
                        }
                    }
                ]
            }
        ]
        me.callParent(arguments);
    },
    afterRender:function(){
        var me = this;
        JE.loadScript('/pro/shop/resourse/css/shop.css')
        me.callParent(arguments);
        me.body.on('click',function(e){
            if(e.getTarget('.changePaystyle',1)){
                me.up('window').close();
                me._thisMe.up('window')?me._thisMe.up('window').close():'';
                var getWaitingOrder =  me.getWaitingOrder();
                JE.win({
                    title:'订单确认',
                    layout:'fit',
                    width:840,//JE.getBodyWidth()-750,
                    height:(JE.getBodyHeight()-250)>=563?JE.getBodyHeight()-250:563,//563
                    border:0,
                    bodyPadding:'20px 40px',
                    default:{border:0},
                    items:[{
                        xtype:'shop.shoppayview',
                        resData:me.resData,
                        winName:me.winName,
                        name:me.name,
                        saveOrderId:me.orderId,
                        selectedPre:getWaitingOrder.selectedPre,
                        selectedSchemeId:getWaitingOrder.selectedSchemeId,
                        payTypeCode:getWaitingOrder.payTypeCode,
                        useBalance:getWaitingOrder.useBalance,
                        beforeSel:getWaitingOrder.beforeSel
                    }]
                }).show();
            }
            if(e.getTarget('.recharge',1)){
                //立即充值
                var win = JE.win({
                    title:'账户充值',
                    width:800,//JE.getBodyWidth()-750,
                    height:600,//JE.getBodyHeight()-235,
                    border:0,
                    draggable : false,
                    default:{border:0},
                    items:[{xtype:'shop.accountpay',orderId:me.orderId}]
                }).show();
                win.on('close',function(){
                    var resData = JE.ajax({
                        url:'/je/shoppingmall/bill/getAccountDetail',
                        params:{}
                    })
                    if(resData.success){
                        var data = Ext.getCmp('shopPayStyleView').getData//me.getData;
                        data.accountBalance = me.setNumStyle(resData.obj.currentAccountBalance);
                        //处理能不能直接支付
                        if(Number(data.orderPay)>Number(data.accountBalance)){
                            data.notenough=true;
                            Ext.getCmp('shopPayStyleView').getCmp('payStyle').getCmp('getVerBtn').hide();
                        }else{
                            data.notenough=false;
                            Ext.getCmp('shopPayStyleView').getCmp('payStyle').getCmp('getVerBtn').show();

                        }
                        var payStyleCon = me.down('[itemId=payStyleCon]');
                        payStyleCon.update(data);
                        //data.notenough?Ext.getCmp('shopPayStyleView').getCmp('payStyle').getCmp('getVerBtn').hide():Ext.getCmp('shopPayStyleView').getCmp('payStyle').getCmp('getVerBtn').show();
                    }
                })
            }
        })

    },
    getData:function(){
        var me = this;
        //此处获取账户信息，和订单信息

        var accountInfo = JE.ajax({
            url:'/je/shoppingmall/bill/getAccountInfo',
            params:{}
        })
        var orderInfo = JE.ajax({
            url:'/je/shoppingmall/bill/getOrder',
            params:{orderId:me.orderId}
        });
        if(orderInfo.success&&accountInfo.success){
            var notenough;
            var orderPay = orderInfo.obj.values.ORDER_PAYMENT;
            var accountBalance =accountInfo.obj.values.ACCOUNT_BALANCE;
            if(orderInfo.obj.values.ORDER_PAYTYPE_CODE!=10){
                notenough = false
            }else{
                if(Number(accountBalance)-Number(orderPay)>=0){
                    notenough = false
                }else{
                    notenough = true
                }
            }

            var data = {
                accountInfo:accountInfo.obj.values,
                orderInfo:orderInfo.obj.values,
                notenough:notenough,
                orderPay:me.setNumStyle(orderPay),
                accountBalance:me.setNumStyle(accountBalance),
                verification:'',
                verificationtext:''
            }
        }
        return data
    },
    getVer:function(btn){
        var me = this;
        if(!btn.isClick)return;
        var getData = me.getData;
        var res = JE.ajax({
            url:'/je/shoppingmall/bill/sendValidateCodeToPhone',
            params:{
                orderId:me.orderId
            }
        })
        if(res.success){
            var s = 60;
            var timer = setInterval(function(){
                s--;
                if(s==0){
                    btn.setText('重新获取');
                    clearInterval(timer);
                    btn.isClick = true;
                    getData.verificationtext = '';
                    btn.previousNode()?btn.previousNode().update(getData):'';
                }else{
                    btn.isClick = false;
                    btn.setText(s+'s');
                }

            },1000);
        }
        getData.verificationtext = res.obj;
        btn.previousNode().update(getData);
    },
    getWaitingOrder:function(){
        var me = this;
        var selectedSchemeId,payTypeCode,payTypeName,year,needPay;
        var data = JE.ajax({
            url:'/je/shoppingmall/bill/getWaitingOrder',
            params:{
                orderId:me.orderId
            }
        })
        if(data.success){
            var resData = data.obj;
            var ACCOUNT_BALANCE =data.obj.accountInfo.values.ACCOUNT_BALANCE;
            Ext.each(resData.schemeInfo,function(itemData){
                if(itemData.values.choosed=='true'){
                    selectedSchemeId = itemData.values.schemeId;
                    year = itemData.values.timeLength+itemData.values.timeUnitName;
                    needPay = itemData.values.schemePrice
                }
            })
            Ext.each(resData.payTypesInfo,function(itemData){
                if(itemData.values.choosed=='true'){
                    payTypeCode = itemData.values.payTypeCode;
                    payTypeName = itemData.values.payTypeName;
                }
            })
        }
        var obj = {
            selectedSchemeId:selectedSchemeId,
            payTypeCode:payTypeCode,
            useBalance:data.obj.useBalance,
            selectedPre:data.obj.limitNumbersInput||'',
            beforeSel:{
                payTypeName:payTypeName,
                year:year,
                payTypeCode:payTypeCode,
                selected:data.obj.useBalance?true:false,
                needPay:me.getNeedPay(payTypeCode,data.obj.useBalance)?needPay:Number(ACCOUNT_BALANCE)>=Number(needPay)?0:Math.abs((Number(ACCOUNT_BALANCE)*100-Number(needPay)*100)*0.01)
            }
        }
        return obj
    },
    getNeedPay:function(type,useBalance){
        if(type==10){
            return true
        }else{
            if(useBalance){
                return false
            }else{
                return true
            }
        }

    },
    setNumStyle:function(num){
        num = num.toString();
        if(num.includes('.')){
           if(num.substr(num.indexOf('.')+1).length==2){
               return num
           }else{
           return num = num+'0';
           }
        }else{
           return num= num+'.00';
        }
    }
});
